/**
 * BomberBoy, CPS240 Term Project - Team A
 * Andrew Osterink, David Bash, Scott Kidder
 * 	
 * This map controls and renders a tile map. Currently very simple as
 * it creates a square tile map of given sizes. 
 * 
 * 
 */

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;

import javax.imageio.ImageIO;

/**
 * @author Andy
 */
public class MapBackup {
	
	public final int TILESIZE = 30;
	
	private Tile[][] tiles;
	
	public MapBackup()
	{
		createMap(10,10);
	}
	
	/**
	 * Creates a default blank map with the default tile type
	 * @param width size of map
	 * @param height size of map
	 */
	public void createMap(int width,int height)
	{
		tiles = new Tile[width][height];
		Image img = ResourceManager.getManager().load("grass11.jpg");
		for(int x = 0;  x < width; x++)
			for(int y = 0; y < height; y++)
			{
				Tile t = new Tile();
				t.setPosition(new Point(x * TILESIZE, y * TILESIZE));
				t.setSize(new Point(TILESIZE,TILESIZE));
				t.setTexture(img);
						
				tiles[x][y] = t;
			}
	}
	
	private void loadFromFile(String fileName)
	{
	
		
	}
	
	public void render(Graphics2D g)
	{	
		//Render back to front  
		for(int x = 0; x < getWidth(); x++)
			for(int y = getHeight() - 1; y > 0; y--)
			{
				Tile t = tiles[x][y];
				t.render(g);
			}
			
	}
	
	
	/**
	 *  returns tile at map coordinate. returns null if invalid.
	 * @param x x position of tile.
	 * @param y y position of tile
	 * @return tile corresponding to coordinates. If  either position exceeds dimensions of map
	 *  it returns null 
	 */
	public Tile getTile(int x,int y)
	{
		if(x < getWidth() && x >= 0 && 
		   y < getHeight() && y >= 0)
			return tiles[x][y];
		return null;
	}
	
	
	//To note: this hasen't been tested yet although im fairly sure its right
	public Tile tileAtPoint(int x,int y)
	{
		int tileX =(int)( x/(double)TILESIZE);
		int tileY = (int) (y / (double)TILESIZE);
	    return getTile(tileX,tileY);
	}
	
	public int getWidth(){
		return tiles.length;
	}
	
	public int getHeight(){
		return tiles[0].length;
	}
}
